package com.example.stroebackstage.servlet;
import com.example.stroebackstage.entity.Administrator;
import com.example.stroebackstage.exception.AdminException;
import com.example.stroebackstage.service.AdminService;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
public class AdminServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        String toDo = req.getServletPath();
//        展示全部
        if (toDo.equals("/toShowAllAdmin")){
            ArrayList<Administrator> administratorList = new AdminService().selectAll();
            req.getSession().setAttribute("administratorList", administratorList);
        }
//        查询用户
        if (toDo.equals("/toFindAdmin")){
            String BySth = req.getParameter("by");
            String massage = req.getParameter("search");
            if("ID".equals(BySth)){
                int id = Integer.parseInt(massage);
                ArrayList<Administrator> administratorList = new AdminService().selectById(id);
                req.getSession().setAttribute("administratorList", administratorList);
            }
            if("name".equals(BySth)){
                ArrayList<Administrator> administratorList = new AdminService().selectByName(massage);
                req.getSession().setAttribute("administratorList", administratorList);
            }
        }
//        删除用户
        if (toDo.equals("/toDelAdmin")){
            String sid = req.getParameter("id");
            int id = Integer.parseInt(sid);
            boolean check = new AdminService().del(id);
            if (check){
                ArrayList<Administrator> administratorList = new AdminService().selectAll();
                req.getSession().setAttribute("administratorList", administratorList);
            }
        }
//        修改用户名
        if (toDo.equals("/toUpdateAdminName")){
            String sid = req.getParameter("adminId");
            String name = req.getParameter("name");
            int id = Integer.parseInt(sid);
            boolean check = false;
            try {
                check = new AdminService().updateName(id,name);
            } catch (AdminException e) {
                e.printStackTrace();
            }
            if (check){
                ArrayList<Administrator> administratorList = new AdminService().selectAll();
                req.getSession().setAttribute("administratorList", administratorList);
            }
        }
//        修改用户角色
        if (toDo.equals("/toUpdateAdminRole")){
            String said = req.getParameter("adminId");
            String srid = req.getParameter("roleId");
            int aid = Integer.parseInt(said);
            int rid= Integer.parseInt(srid);
            boolean check = new AdminService().updateRole(aid,rid);
            if (check){
                ArrayList<Administrator> administratorList = new AdminService().selectAll();
                req.getSession().setAttribute("administratorList", administratorList);
            }
        }
//        添加用户
        if (toDo.equals("/toInsertAdmin")){
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            String sRoleID = req.getParameter("roleID");
            int roleID = Integer.parseInt(sRoleID);
            Administrator administrator = new Administrator(name,password,roleID);
            boolean check = false;
            try {
                check = new AdminService().insert(administrator);
            } catch (AdminException e) {
                e.printStackTrace();
            }
            if (check){
                ArrayList<Administrator> administratorList = new AdminService().selectAll();
                req.getSession().setAttribute("administratorList", administratorList);
            }

        }
//        重定向到管理员用户页面
        resp.sendRedirect(req.getContextPath()+"/administrator.jsp");
    }
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    doGet(req,resp);
    }
}
